package com.canaan.business.service.invoice.impl;

import com.canaan.business.common.enums.inOutStock.OutOrderStatusEnum;
import com.canaan.business.domain.basic.WmsProduct;
import com.canaan.business.domain.invoice.WmsSalesOrder;
import com.canaan.business.domain.invoice.WmsSalesOrderDetail;
import com.canaan.business.mapper.invoice.WmsSalesOrderDetailMapper;
import com.canaan.business.service.basic.IWmsProductService;
import com.canaan.business.service.invoice.IWmsSalesOrderDetailService;
import com.canaan.business.service.invoice.IWmsSalesOrderService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 销售订单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-13
 */
@Service
public class WmsSalesOrderDetailServiceImpl implements IWmsSalesOrderDetailService
{
    @Resource
    private WmsSalesOrderDetailMapper wmsSalesOrderDetailMapper;

    @Resource
    private IWmsProductService wmsProductService;

    @Resource
    private IWmsSalesOrderService wmsSalesOrderService;

    /**
     * 查询销售订单明细
     *
     * @param id 销售订单明细主键
     * @return 销售订单明细
     */
    @Override
    public WmsSalesOrderDetail selectWmsSalesOrderDetailById(Long id)
    {
        return wmsSalesOrderDetailMapper.selectWmsSalesOrderDetailById(id);
    }

    /**
     * 查询销售订单明细列表
     *
     * @param wmsSalesOrderDetail 销售订单明细
     * @return 销售订单明细
     */
    @Override
    public List<WmsSalesOrderDetail> selectWmsSalesOrderDetailList(WmsSalesOrderDetail wmsSalesOrderDetail)
    {
        return wmsSalesOrderDetailMapper.selectWmsSalesOrderDetailList(wmsSalesOrderDetail);
    }

    /**
     * 新增销售订单明细
     *
     * @param wmsSalesOrderDetail 销售订单明细
     * @return 结果
     */
    @Override
    public int insertWmsSalesOrderDetail(WmsSalesOrderDetail wmsSalesOrderDetail)
    {
        //获取销售订单信息
        WmsSalesOrder order = new WmsSalesOrder();
        order.setNo(wmsSalesOrderDetail.getOrderNo());
        List<WmsSalesOrder> orders = wmsSalesOrderService.selectWmsSalesOrderList(order);
        if(orders.isEmpty()){
            throw new ServiceException("销售订单编号不存在");
        }
        order = orders.get(0);
        //校验物料编号
        checkoutProduct(wmsSalesOrderDetail);
        //更新销售订单总数
        order.setTotalOrderQty(order.getTotalOrderQty() + wmsSalesOrderDetail.getOrderQty());
        wmsSalesOrderService.updateWmsSalesOrder(order);
        //新增订单明细
        wmsSalesOrderDetail.setOrderDetailStatus(OutOrderStatusEnum.NEW.getCode());
        wmsSalesOrderDetail.setNo(TableNoUtil.getNo(WmsSalesOrderDetail.class));
        wmsSalesOrderDetail.setCreateBy(SecurityUtils.getUsername());
        wmsSalesOrderDetail.setCreateTime(DateUtils.getNowDate());

        return wmsSalesOrderDetailMapper.insertWmsSalesOrderDetail(wmsSalesOrderDetail);
    }

    /**
     * 修改销售订单明细
     *
     * @param wmsSalesOrderDetail 销售订单明细
     * @return 结果
     */
    @Override
    public int updateWmsSalesOrderDetail(WmsSalesOrderDetail wmsSalesOrderDetail)
    {
        //获取销售订单信息
        WmsSalesOrder order = new WmsSalesOrder();
        order.setNo(wmsSalesOrderDetail.getOrderNo());
        List<WmsSalesOrder> orders = wmsSalesOrderService.selectWmsSalesOrderList(order);
        if(orders.isEmpty()){
            throw new ServiceException("销售订单编号不存在");
        }
        //更新销售订单总数
        order = orders.get(0);
        WmsSalesOrderDetail old = wmsSalesOrderDetailMapper.selectWmsSalesOrderDetailById(wmsSalesOrderDetail.getId());
        if(!old.getOrderQty().equals(wmsSalesOrderDetail.getOrderQty())) {
            order.setTotalOrderQty(order.getTotalOrderQty() + (wmsSalesOrderDetail.getOrderQty() - old.getOrderQty()));
            wmsSalesOrderService.updateWmsSalesOrder(order);
        }
        //校验物料编号
        checkoutProduct(wmsSalesOrderDetail);

        wmsSalesOrderDetail.setUpdateBy(SecurityUtils.getUsername());
        wmsSalesOrderDetail.setUpdateTime(DateUtils.getNowDate());
        return wmsSalesOrderDetailMapper.updateWmsSalesOrderDetail(wmsSalesOrderDetail);
    }

    /**
     * 批量删除销售订单明细
     *
     * @param ids 需要删除的销售订单明细主键
     * @return 结果
     */
    @Override
    public int deleteWmsSalesOrderDetailByIds(Long[] ids)
    {
        WmsSalesOrderDetail detail = new WmsSalesOrderDetail();
        detail.setIds(Arrays.asList(ids));
        List<WmsSalesOrderDetail> details = wmsSalesOrderDetailMapper.selectWmsSalesOrderDetailList(detail);
        boolean isNotNew = details.stream()
                .anyMatch(e -> !OutOrderStatusEnum.NEW.getCode().equals(e.getOrderDetailStatus()));
        if(isNotNew){
            throw new ServiceException("有部分明细不在新建状态不允许删除");
        }

        return wmsSalesOrderDetailMapper.deleteWmsSalesOrderDetailByIds(ids);
    }

    /**
     * 删除销售订单明细信息
     *
     * @param id 销售订单明细主键
     * @return 结果
     */
    @Override
    public int deleteWmsSalesOrderDetailById(Long id)
    {
        return wmsSalesOrderDetailMapper.deleteWmsSalesOrderDetailById(id);
    }

    /**
     * 根据销售订单编号批量删除销售订单明细
     *
     * @param orderNos 销售订单编号
     * @return 结果
     */
    @Override
    public int deleteByOrderNos(List<String> orderNos) {
        return wmsSalesOrderDetailMapper.deleteByOrderNo(orderNos);
    }

    /**
     * 检验物料
     * @param entity
     */
    private void checkoutProduct(WmsSalesOrderDetail entity){

        //校验物料和物料是否重复
        WmsProduct product = wmsProductService.selectWmsProductByProductNo(entity.getProductNo());
        if(product == null){
            throw new ServiceException("物料编号不存在");
        }
        entity.setProductUnit(product.getCompany());
        WmsSalesOrderDetail params = new WmsSalesOrderDetail();
        params.setOrderNo(entity.getOrderNo());
        List<WmsSalesOrderDetail> details = wmsSalesOrderDetailMapper.selectWmsSalesOrderDetailList(params);
        if(!details.isEmpty()){
            WmsSalesOrderDetail old =details.stream()
                    .filter(e -> e.getProductNo().equals(entity.getProductNo())
                            && (entity.getId() == null || !entity.getId().equals(e.getId())))
                    .findFirst().orElse(null);
            if(old != null){
                throw new ServiceException("物料编号已存在，不能重复添加");
            }
        }
    }

    /**
     * 根据销售订单编号查询出列表
     * @param orderNos 销售订单编号列表
     * @return 数据列表
     */
    @Override
    public List<WmsSalesOrderDetail> listByOrderNos(List<String> orderNos) {
        return wmsSalesOrderDetailMapper.listByOrderNos(orderNos);
    }

    @Override
    public int batchInsertSalesOrderDetail(List<WmsSalesOrderDetail> details) {
        for (WmsSalesOrderDetail wmsSalesOrderDetail : details) {
            wmsSalesOrderDetail.setOrderDetailStatus(OutOrderStatusEnum.NEW.getCode());
            wmsSalesOrderDetail.setNo(TableNoUtil.getNo(WmsSalesOrderDetail.class));
            wmsSalesOrderDetail.setCreateBy(SecurityUtils.getUsername());
            wmsSalesOrderDetail.setCreateTime(DateUtils.getNowDate());        }

        return wmsSalesOrderDetailMapper.batchInsertSalesOrderDetail(details);
    }
}
